<?php

/**
 * alumno actions.
 *
 * @package    ecampus
 * @subpackage alumno
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class alumnoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
      $this->redirect('homepage');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($this->alumno);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AlumnoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AlumnoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    
    $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario'))), sprintf('Object alumno does not exist (%s).', $request->getParameter('id_usuario')));
//    $this->form = new AlumnoForm($alumno);
    $this->form = new EditarAlumnoForm($alumno);
  }

  public function executeUpdate(sfWebRequest $request)
  {
     $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));

    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario'))), sprintf('Object alumno does not exist (%s).', $request->getParameter('id_usuario')));
   // $this->form = new AlumnoForm($alumno);
   $this->form = new EditarAlumnoForm($alumno);
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');

  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario'))), sprintf('Object alumno does not exist (%s).', $request->getParameter('id_usuario')));
    $alumno->delete();

    $this->redirect('alumno/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $id_usuario = $request->getParameter('id_usuario');
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $alumno = $form->save();

//      $this->redirect('alumno/index?id_usuario='.$alumno->getIdUsuario());
    $this->redirect('alumno/index?id_usuario='.$id_usuario);
    // $this->redirect('alumno/index');
    }
  }
  public function executeCursos(sfWebRequest $request)
  {
      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
      $this->cursos = $this->alumno->getAsignacionesDeCursos()->execute();
//      $this->cursos = Doctrine::getTable('Asignacionalumno')
//              ->createQuery('aa')
//              ->where('aa.id_alumno = ? and aa.activo = ?',array($request->getParameter('id_usuario'),true))
//              ->execute();
  }
  public function executeAlertas(sfWebRequest $request)
  {
      $this->borrar = $request->getParameter('borrar');
      $this->marcar_leido = $request->getParameter('marcar_leido');
      $this->tipoUsuario = 'alumno';
      
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
      $this->usuario = $this->alumno;
//      $this->alertas = Doctrine_Core::getTable('Usuario')->getAlertas($request->getParameter('id_usuario'));
      $this->alertas = Doctrine_Core::getTable('Alerta')->findBy('id_usuario', $request->getParameter('id_usuario'));

      $this->alertasNoLeidas = Doctrine_Core::getTable('Usuario')->getAlertasNoLeidas($request->getParameter('id_usuario'));
   }
   public function executeDatos(sfWebRequest $request)
  {
    $this->id_usuario = $request->getParameter('id_usuario');
    $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
    $this->forward404Unless($alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario'))), sprintf('Object alumno does not exist (%s).', $request->getParameter('id_usuario')));
    $this->datos = new AlumnoForm($alumno);
  }

  public function executeGrupos(sfWebRequest $request)
  {   
      $this->id_usuario = $request->getParameter('id_usuario');
      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
      $this->grupos = Doctrine::getTable('Asignaciongrupo')
              ->createQuery('aa')
              ->where('aa.id_alumno = ?',$request->getParameter('id_usuario'))
              ->execute();
  }

  public function executeAsistencia(sfWebRequest $request)
  {
      $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
//      $this->asistencias = Doctrine::getTable('Asistencia')
//              ->createQuery('a')
//              ->where('a.id_alumno = ?',$request->getParameter('id_usuario'))
//              ->execute();


      $this->pager = new sfDoctrinePager('Asistencia', '5');
      $this->pager->setQuery(Doctrine::getTable('Asistencia')
                            ->createQuery('a')
              ->where('a.id_alumno = ?',$request->getParameter('id_usuario')));
      $this->pager->setPage($request->getParameter('page', 1));
      $this->pager->init();

















 }
   public function executeMensajes(sfWebRequest $request)
   {
       $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
       $this->mensajes = MensajeTable::getMensajesDeUsuario($request->getParameter('id_usuario'));
   }


   public function executeHorarios(sfWebRequest $request)
   {
       $this->id_alumno = $request->getParameter('id_usuario');
       $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));

       $this->cursos = Doctrine::getTable('Asignacionalumno')
              ->createQuery()
              ->select('a.id_alumno, a.id_curso as id_curso, c.nombre AS nombre')
              ->from ('Asignacionalumno a')
              ->leftJoin('a.Curso c')
              ->where('a.id_alumno = ?',$request->getParameter('id_usuario'))
              ->execute();

//       $this->id_alumno = $request->getParameter('id_usuario');
//       $this->alumno = Doctrine::getTable('Alumno')->find(array($request->getParameter('id_usuario')));
//       $this->cursos = Doctrine::getTable('Asignacionalumno')
//              ->createQuery('aa')
//              ->where('aa.id_alumno = ?',$request->getParameter('id_usuario'))
//              ->execute();
//
//       $this->a = Doctrine_Query::create()
//               ->select('hc.id_horario id')
//               ->from('horariocurso hc')
////               ->leftJoin('curso c')
////               ->innerJ
////               ->fetchArray();
//               ;

//       $this->horariosCurso = Doctrine_Query::create()
////               ->createQuery('hc')
//               ->select('hc.dia dia,c.nombre nombre ')
//               ->from('horariocurso hc')
//               ->leftJoin('Curso c')
//                   ->leftJoin('asignacionalumno aa')
//               ->where('aa.id_alumno = ?',$this->id_alumno)
////                       ->execute()
////                       ->fetchArray()
//               ;
   }
   public function executeBoletin(sfWebRequest $request)
   {
       $this->id_alumno = $request->getParameter('id_usuario');
       $this->alumno = Doctrine_Core::getTable('Alumno')->find($request->getParameter('id_usuario'));
       $this->cursos = $this->alumno->getAsignacionesDeCursos()->execute();
   }

   public function executeFecha(sfWebRequest $request)
   {
       $this->id_alumno = $request->getParameter('id_usuario');
       $this->alumno = Doctrine_Core::getTable('Alumno')->find($request->getParameter('id_usuario'));
       $this->cursos = $this->alumno->getAsignacionesDeCursos()->execute();
       
   }

   public function executeCalificaciones(sfWebRequest $request)
   {
       $this->id_alumno = $request->getParameter('id_usuario');
       $this->id_usuario = $this->id_alumno;
       $this->alumno = Doctrine_Core::getTable('Alumno')->find($request->getParameter('id_usuario'));

//       $this->calificaciones = $this->alumno->get

       $this->cursos = $this->alumno->getAsignacionesDeCursos()->execute();
   }
   public function executeDetalleCurso(sfWebRequest $request)
   {
       $this->id_usuario = $request->getParameter('id_usuario');
       $this->alumno = Doctrine_Core::getTable('Alumno')->find($request->getParameter('id_usuario'));
       $this->id_curso = $request->getParameter('id_curso');

       $this->curso = Doctrine_Core::getTable('Curso')->find($this->id_curso);
   }

}
